package com.could.minority.model;

import io.rong.ApiHttpClient;
import io.rong.models.FormatType;
import io.rong.models.SdkHttpResult;
import io.rong.util.Constant;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.could.BaseClass;
import com.could.db.DbConn;
import com.could.util.SqlUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class UserFirends extends BaseClass {
	private DbConn db=null;
	
	 public UserFirends(){
	   super(UserFirends.class.getName());
	   db=new DbConn();
	}
	 
	 public UserFirends(DbConn db){
		   super(UserFirends.class.getName());
		   this.db = db;
		}
	public DbConn getDb() {
		  return db;
	 }
	public void setDb(DbConn db) {
		this.db = db;
	 }
	
	public String getFriends(String userid){
		String result = "";
		LinkedList list = new LinkedList();
		SqlUtil sqlutil = new SqlUtil();
		String strsql = "select friendid from user_friends where userid = ?";
		sqlutil.setSql(strsql);
		String params = userid;
		try {
			list =sqlutil.executeParamList(params);
			HashMap<String, String> map = new HashMap<String, String>();
			JsonObject jsonObject = new JsonObject();
			SdkHttpResult result1 = null;
			if(list.size()>0){
				JsonArray Friends = new JsonArray();
				JsonObject json = null;
				for (int i = 0; i < list.size(); i++) {
					map = (HashMap) list.get(i);
					String friendid = map.get("friendid");
					  if(!"".equals(friendid)){
						 // SqlUtil sqlu  = new SqlUtil();
						  String strsqls = "select userid id,email,username,portrait,status from user where userid = ?";
						  sqlutil.setSql(strsqls);
						  String para = friendid;
						  HashMap<String,Object> friendsMap = (HashMap<String, Object>) sqlutil.executeParamList(para).get(0);
						  if(friendsMap!=null&&friendsMap.size()>0){
						 json = new JsonObject();
						 json.addProperty("id", friendsMap.get("id").toString());
						 json.addProperty("email", friendsMap.get("email").toString());
						 json.addProperty("username", friendsMap.get("username").toString());
						 json.addProperty("portrait", friendsMap.get("portrait").toString());
						 json.addProperty("status", friendsMap.get("status").toString());
						 Friends.add(json);
						  }
					  }
				}
				 result1 = new SdkHttpResult(200, Friends.toString());
			}else{
				jsonObject.addProperty("message", "email or password is error");
				 result1 = new SdkHttpResult(104, jsonObject.toString());
			}
			result = result1.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result ;
		
	}
	
	
}
